package cn.sym.pet.controller;

import cn.sym.basic.exception.MyException;
import cn.sym.basic.jwt.LoginData;
import cn.sym.basic.util.AjaxResult;
import cn.sym.basic.util.LoginContext;
import cn.sym.basic.util.PageList;
import cn.sym.org.domain.Employee;
import cn.sym.pet.domain.Pet;
import cn.sym.pet.domain.PetDetail;
import cn.sym.pet.query.PetQuery;
import cn.sym.pet.service.IPetDetailService;
import cn.sym.pet.service.IPetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/pet")
public class PetController {
    @Autowired
    private IPetService petService;
    @Autowired
    private IPetDetailService iPetDetailService;


    /**
     * 保存和修改公用的
     * @param pet  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Pet pet,HttpServletRequest request){
        System.out.println(pet);
        LoginData login = (LoginData) LoginContext.getLogin(request);
        Employee employee = login.getAdmin();
        try {
            petService.addOrUpdate(pet,employee);
            return AjaxResult.my();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.my().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }

    /**
     * 宠物上架
     * @param ids
     * @return
     */
    @PostMapping("/onSale")
    public AjaxResult onsale(@RequestBody List<Long> ids) {
        System.out.println(ids);
        try {
            petService.onSale(ids);
            return AjaxResult.my();
        } catch (MyException e) {//捕获我们自己的业务异常
            e.printStackTrace();
            return AjaxResult.my().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.my().setSuccess(false).setMessage("系统繁忙，稍后重试！！");
        }
    }

    /**
     * 宠物下架
     * @param ids
     * @return
     */
    @PostMapping("/offSale")
    public AjaxResult offsale(@RequestBody List<Long> ids){
        try {
            petService.offSale(ids);
            return AjaxResult.my();
        } catch (MyException e) {//捕获我们自己的业务异常
            e.printStackTrace();
            return AjaxResult.my().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.my().setSuccess(false).setMessage("系统繁忙，稍后重试！！");
        }

    }

    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            petService.remove(id);
            return AjaxResult.my();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.my().setSuccess(false).setMessage("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public Pet get(@PathVariable("id")Long id)
    {
        return petService.queryById(id);
    }

    //获取详情
    @GetMapping("/detail/{id}")
    public PetDetail detail(@PathVariable("id")Long id) {
        System.out.println(id);
        Pet pet = petService.queryById(id);
        return iPetDetailService.queryById(pet.getId());
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Pet> list(){

        return petService.queryAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<Pet> json(@RequestBody PetQuery query) {
        return petService.queryPage(query);
    }


}
